package com.maori.languageapp.curateword;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.maori.languageapp.LanguageAppCaptureModify;
import com.maori.languageapp.R;
import com.maori.languageapp.consts.ImageLoader;
import com.maori.languageapp.consts.UrlContant;
import com.maori.languageapp.curatestory.Maori_Curate_Story;
import com.maori.languageapp.curatestory.Maori_Curate_Story_Details;
import com.maori.languageapp.curateword.Maori_Curate_Word_Details.loadSingleViewSynImage;
import com.maori.languageapp.curateword.Maori_Curate_Word_Details.sendToApproval;
import com.maori.languageapp.curateword.Maori_Curate_Word_Details.sendToReject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Maori_Curate_Word_Details_Adapter extends BaseAdapter {
	private static final String TAG = null;
	// Declare Variables
	Context context;
	LayoutInflater inflater;
	ArrayList<HashMap<String, String>> data;
	ImageLoader imageLoader;
	String SYNNID, result, NodeApproveA = "0", NodeApproveB = "0";
	String saveNodeApprove = UrlContant.saveNodeApprove;
	String saveNodeReject = UrlContant.saveNodeReject;
	HashMap<String, String> resultp = new HashMap<String, String>();
	HttpParams httpParameters = new BasicHttpParams();
	int timeoutConnection = 5000;
	InputStream is = null;
	StringBuilder sb = null;
	JSONArray jsonarray;

	public Maori_Curate_Word_Details_Adapter(Context context,
			ArrayList<HashMap<String, String>> arraylist) {
		this.context = context;
		data = arraylist;
		imageLoader = new ImageLoader(context);

	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// Declare Variables
		TextView maori_synonym_title, english_synonym_title, first_name, last_name;
		ImageView flag; // Loading User Images (Future use)
		Button dicover_example_country, Discover_example_likes;
		final Button curate_approve;
		final Button curate_reject;
		final Button curate_modify;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View itemView = inflater.inflate(R.layout.languageapp_curate_synonyms,
				parent, false);

		// Get the position from the results

		resultp = data.get(position);
		// flag = (ImageView) itemView.findViewById(R.id.mem_discover_img);

		// Display Synonyms
		maori_synonym_title = (TextView) itemView
				.findViewById(R.id.maori_synonym_title);
		english_synonym_title = (TextView) itemView
				.findViewById(R.id.english_synonym_title);
		dicover_example_country = (Button) itemView
				.findViewById(R.id.dicover_example_country);
		first_name = (TextView) itemView.findViewById(R.id.first_name);
		last_name = (TextView) itemView.findViewById(R.id.last_name);
		flag = (ImageView) itemView.findViewById(R.id.imggriditem);

		curate_approve = (Button) itemView.findViewById(R.id.curate_approve);
		curate_reject = (Button) itemView.findViewById(R.id.curate_reject);
		curate_modify = (Button) itemView.findViewById(R.id.curate_modify);

		// Capture position and set results to the TextViews
		maori_synonym_title.setText(resultp
				.get(Maori_Curate_Word_Details.Description_a));
		english_synonym_title.setText(resultp
				.get(Maori_Curate_Word_Details.Description_b));
		dicover_example_country.setText(resultp
				.get(Maori_Curate_Word_Details.tribeuser));
		first_name.setText(resultp
				.get(Maori_Curate_Word_Details.first_namesuser));
		last_name.setText(resultp.get(Maori_Curate_Word_Details.last_nameuser));

		NodeApproveA = resultp.get(Maori_Curate_Word_Details.node_active_a);
		NodeApproveB = resultp.get(Maori_Curate_Word_Details.node_active_b);
		Log.e("PRINT 1",
				" :" + resultp.get(Maori_Curate_Word_Details.node_active_a));
		Log.e("PRINT 2",
				" :" + resultp.get(Maori_Curate_Word_Details.node_active_b));
		if (NodeApproveA.equals("1")) {
			curate_approve.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_tick, 0, 0, 0);
		} else if (NodeApproveA.equals("3")) {
			curate_reject.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_cross, 0, 0, 0);
		} else if (NodeApproveA.equals("4")) {
			curate_modify.setCompoundDrawablesWithIntrinsicBounds(
					R.drawable.b_pencil, 0, 0, 0);
		}


		imageLoader.DisplayImage(
				resultp.get(Maori_Curate_Word_Details.userimageuser), flag);
		curate_approve.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				SYNNID = resultp.get(Maori_Curate_Word_Details.SYNNID);
				Log.e("PRINT 1",
						" :" + resultp.get(Maori_Curate_Word_Details.SYNNID));
				Log.e("PRINT 1",
						" :" + resultp.get(Maori_Curate_Word_Details.UIDuser));
				if (!NodeApproveA.equals("1")) {
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.b_tick, 0, 0, 0);
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_cross, 0, 0, 0);
					curate_modify.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_pencil, 0, 0, 0);
				} else {
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_tick, 0, 0, 0);
				}
				new sendToApproval().execute();
			}
		});

		curate_reject.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Log.e("PRINT 2",
						" :" + resultp.get(Maori_Curate_Word_Details.SYNNID));
				Log.e("PRINT 2",
						" :" + resultp.get(Maori_Curate_Word_Details.UIDuser));
				if (!NodeApproveA.equals("3")) {
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.b_cross, 0, 0, 0);
					curate_approve.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_tick, 0, 0, 0);
					curate_modify.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_pencil, 0, 0, 0);
				} else {
					curate_reject.setCompoundDrawablesWithIntrinsicBounds(
							R.drawable.g_cross, 0, 0, 0);
				}
				new sendToReject().execute();
			}
		});

		curate_modify.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Log.e("PRINT 3",
						" :" + resultp.get(Maori_Curate_Word_Details.SYNNID));
				Log.e("PRINT 3",
						" :" + resultp.get(Maori_Curate_Word_Details.UIDuser));

				HashMap<String, String> resultp = new HashMap<String, String>();
				resultp = data.get(position);
				// Send single item click data to SingleItemView Class
				Intent intent = new Intent(context,
						LanguageAppCaptureModify.class);

				intent.putExtra("UpdateNID",
						resultp.get(Maori_Curate_Word_Details.SYNNID));
				intent.putExtra("MID",
						resultp.get(Maori_Curate_Word_Details.MID));
				intent.putExtra("type", "Word");
				intent.putExtra("MainNodeDetails", "");
				context.startActivity(intent);

			}
		});

		return itemView;
	}

	/**************** Uploading Images Via FTP ************************/
	class sendToApproval extends AsyncTask<Void, Void, Void> {

		private final String TAG = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			ArrayList<NameValuePair> nameValuePairsMainW = new ArrayList<NameValuePair>();
			try {
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(saveNodeApprove);
				JSONObject json = new JSONObject();
				json.put("record1", SYNNID);
				JSONArray postjson = new JSONArray();
				postjson.put(json);

				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsMainW));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
					if (result != null) {
						Log.e("REQUEST :", "RESULT***** =" + result);
						try {
							JSONObject jsonObj = new JSONObject(result);
							jsonarray = jsonObj.getJSONArray("posts");
							for (int i = 0; i < jsonarray.length(); i++) {
								JSONObject c = jsonarray.getJSONObject(i);
								// Get the result of rank
								NodeApproveA = c.getString("node_active_a");
								NodeApproveB = c.getString("node_active_b");
							}
						} catch (Exception e) {

						}
					}

				}

			} catch (Exception e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

		}
	}

	/**************** Uploading Images Via FTP ************************/
	class sendToReject extends AsyncTask<Void, Void, Void> {

		private final String TAG = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {

			ArrayList<NameValuePair> nameValuePairsMainW = new ArrayList<NameValuePair>();
			try {
				HttpConnectionParams.setConnectionTimeout(httpParameters,
						timeoutConnection);
				HttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(saveNodeReject);
				JSONObject json = new JSONObject();
				json.put("record1", SYNNID);
				json.put("record2", "Update Synonim");
				json.put("record3", "Update Synonim");

				JSONArray postjson = new JSONArray();
				postjson.put(json);

				httppost.setHeader("json", json.toString());
				httppost.getParams().setParameter("jsonpost", postjson);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsMainW));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				if (is != null) {

					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "iso-8859-1"), 8);
					sb = new StringBuilder();
					sb.append(reader.readLine() + "\n");
					String line = "0";

					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					is.close();
					result = sb.toString();
					if (result != null) {
						Log.e("REQUEST :", "RESULT***** =" + result);
						try {
							JSONObject jsonObj = new JSONObject(result);
							jsonarray = jsonObj.getJSONArray("posts");
							for (int i = 0; i < jsonarray.length(); i++) {
								JSONObject c = jsonarray.getJSONObject(i);
								// Get the result of rank
								NodeApproveA = c.getString("node_active_a");
								NodeApproveB = c.getString("node_active_b");
							}
						} catch (Exception e) {

						}
					}
				}

			} catch (Exception e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

		}
	}
}
